<?php

define('VIEW_PATH', XF_PATH . 'views' . DIRECTORY_SEPARATOR);


require(XF_PATH . 'app' . DIRECTORY_SEPARATOR . 'Loader.php');

Loader::includeFile('app', 'Config');

Loader::includeFile('app', 'Logger');

Loader::includeFile('app', 'SQL/SQL');

Loader::includeFile('app', 'abstract/AbstractController');
Loader::includeFile('app', 'abstract/AbstractModel');

Loader::includeFile('app', 'URL');
Loader::includeFile('app', 'Layout');

Loader::includeFile('app', 'Inflector');

Loader::includeFile('helper', 'Html');

Loader::includeFile('config', 'app');

$URL = URL::getInstance();

SQLTransaction::open();

$autoLoad = Config::get('autoLoad');
if (isset($autoLoad)) {
    foreach ($autoLoad as $key => $val) {
        switch ($key) {
            case 'controllers':
                foreach ($val as $controller) {
                    Loader::loadController($controller);
                }
                break;
        }
    }
}

$beforeRender = array();

$controllerName = $URL->getController();

$Controller = Loader::loadController($controllerName);

foreach ($Controller->helpers as $helper) {
    Loader::includeFile('helper', $helper);
    $Controller->$helper = new $helper();
    if (method_exists($Controller->$helper, 'beforeRender')) {
        $beforeRender[] = $Controller->$helper;
    }
}

$action = $URL->getAction();

call_user_func(array($Controller, $URL->getAction()), $URL->getParams());

foreach ($beforeRender as $bf) {
    $bf->beforeRender();
}

$Layout = Layout::getInstance();
$Layout->renderLayout();
?>
